﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Web.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        //可以指点让某个用户访问,或者某个组的用户访问
        //[Authorize(Users = "?")]  
        public ActionResult Index()
        {
            var name = User.Identity.Name;

            //获取全部用户列表

            return View();
        }

        public ActionResult Login()
        {
            FormsAuthentication.SignOut();//安全退出
            return View();
        }

        #region ===================内部方法==========================
        /// <summary>
        /// 普通登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="userpwd"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult login(string un, string pwd, string returnUrl)
        {
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
            Common.Json json = new Common.Json();
            DAO.BLL.B_Users b_user = new DAO.BLL.B_Users();
            var m_user = new Domain.Users();
            if (ModelState.IsValid)
            {
                m_user = b_user.Login(un, pwd);
                if (m_user != null)
                {
                    FormsAuthentication.SetAuthCookie(m_user.user_name, true);
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        json.returnUrl = returnUrl;
                    }
                    else
                    {
                        json.returnUrl = "/home/index";
                    }
                }
                else
                {

                    json.msg = "用户名或密码不正确！";
                    json.status = -1;
                }
            }

            return Json(json);
        }
        #endregion ===================内部方法==========================
       
    }
}
